<?php
	//returns 0 or 1 depending on if a book by that name exists, actually should i like throw an error in getBook
	function book_exists($book_name)
	{
		if(!(simplexml_load_file($book_name)))
		{
			return false;
		}
		return true;
	}
	//returns a book object representing the book in that filename
	function get_book($book_name)
	{
		book_XML = simplexml_load_file($book_name);
		
		$attributes = array(); 
		$attributes = $xml->children(); 
		
		var $book_name="";
		var $chapter_names = array();
		var $chapter_numbers = array();
		var $lead_author_email="";
		var $author_list = array();
		var $chapter_text = array();
		
		
		foreach ($attributes as $name => $value)
		{
			//note, SimpleXMLElement is not an object, its a system resourse
			$Attrname = ($name instanceof SimpleXMLElement)?$name->getName():$name; 
			if(strcmp($Attrname,"bookTitle"))
			{
				$book_name=$value;
			}
			if(strcmp($Attrname,"leadAuthorEmail"))
			{
				$lead_author_email=$value;
			}
			if(strcmp($Attrname,"authorEmail"))
			{
				array_push($author_list,$value);
			}
			if($name instanceof SimpleXMLElement)
			{
				$secondAttr = $name->children();
				foreach ($secondAttr as $nameSecond => $valueSecond)
				{
					array_push($chapter_text, array())
					$secondAttr = ($nameSecond instanceof SimpleXMLElement)?$nameSecond->children():$nameSecond;
					if(strcmp($secondAttr,"chapterNumber"))
					{
						array_push($chapter_names,$value);
					}
					if(strcmp($secondAttr,"chapterTitle"))
					{
						array_push($chapter_numbers,$value);
					}
					if(strcmp($secondAttr,"paragraph"))
					{
						array_push($chapter_text,$value);
					}
				}
			}
			
		}
		
		
	}
	
	//note these must be used for writing to file, to ensure that people aren't trying to write to the same file from more than one instance
	//adds a chapter
	function add_book($book_name)
	{
		$XML_str = "";
		$book_XML = new SimpleXMLElement($XML_str);
		book_XML->saveXML();
	}
	//chapter text 
	function add_chapter($book_name,$chapter_name,$chapter_text,$chapter_number)
	{
		book_XML = simplexml_load_file($book_name);
		//$book_XML->addChild('attrname','stuff');
		book_XML->saveXML();
	}
	function modify_chapter($book_name,$chapter_number,$new_text)
	{
		
	}
?>